//
//  HEReporReadDetailsViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/9.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEReporReadDetailsViewController.h"
#import "HEGridImageView.h"
#import "HESpeechManager.h"

@interface HEReporReadDetailsViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UITextView *contentLb;
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) HEGridImageView *gridView;

@end

@implementation HEReporReadDetailsViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[HESpeechManager sharedManager] stopSpeech];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"解读详请";
    
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
    // 标题
    _titleLb = [UILabel new];
    _titleLb.text = _model.report_name;
    _titleLb.font = DYBlodFont(18);
    _titleLb.numberOfLines = 0;
    [self.contentView addSubview:_titleLb];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
 
    _gridView = [[HEGridImageView alloc] init];
    _gridView.imagesArray = [_model.report_url componentsSeparatedByString:@","];
    [self.contentView addSubview: _gridView];
    
    [_gridView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
    }];
    [_gridView shwoViewWithFixedSpacing:10 leadSpacing:10 tailSpacing:10];
  
    
    // 姓名
    UILabel *name = [UILabel new];
    name.text = _model.doc_name;
    name.font = DYBlodFont(18);
    name.numberOfLines = 0;
    [self.contentView addSubview:name];
    
    // 职位
    UILabel *postionLb = [UILabel new];
    postionLb.text = _model.doc_professor;
    postionLb.font = DYBlodFont(16);
    postionLb.textColor = CNavBgColor;
    [self.contentView addSubview:postionLb];
    
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_gridView.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
    }];
    [name setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [postionLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(name.mas_bottom);
        make.left.equalTo(name.mas_right).mas_offset(10);
        make.right.equalTo(self.view).mas_offset(-15);
    }];
    
    
    UIImageView *backView = [UIImageView new];
    backView.image = IMAGE_NAMED(@"report_detail_bg");
    [self.contentView addSubview:backView];

    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.height.offset(120);
    }];
    
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    [self.contentView addSubview:warp];
 
    warp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    warp.layer.shadowOffset = CGSizeMake(0,5);
    warp.layer.shadowOpacity = 1;
    warp.layer.shadowRadius = 6;
    warp.layer.cornerRadius = 10;
 
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).mas_offset(20);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
    
    // 内容
    UIImageView *repIconView = [UIImageView new];
    repIconView.image = IMAGE_NAMED(@"report_detail_read");
    repIconView.contentMode = UIViewContentModeScaleAspectFill;
    [warp addSubview:repIconView];
    
    // 职位
    UILabel *itemTitleLb = [UILabel new];
    itemTitleLb.text = @"报告解读";
    itemTitleLb.font = DYBlodFont(18);
    itemTitleLb.textColor = CNavBgColor;
    [warp addSubview:itemTitleLb];
    
    [repIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(itemTitleLb);
        make.left.equalTo(warp).offset(15);
        make.width.offset(20);
        make.height.offset(24);
    }];
    
    [itemTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).offset(15);
        make.left.equalTo(repIconView.mas_right).mas_offset(15);
    }];
    [itemTitleLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    // 语音
    UIImageView *speeView = [UIImageView new];
    speeView.contentMode = UIViewContentModeScaleAspectFill;
    speeView.image = IMAGE_NAMED(@"guide_voice");
    [warp addSubview:speeView];
    speeView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(speedVioceClick)];
    [speeView addGestureRecognizer:tap];
    
    [speeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(itemTitleLb);
        make.left.equalTo(itemTitleLb.mas_right).offset(10);
        make.width.offset(24);
        make.height.offset(22);
    }];
    
    // 描述
    _contentLb = [UITextView new];
    _contentLb.text = _model.guidance;
    _contentLb.editable=NO;
    _contentLb.scrollEnabled = NO;
    _contentLb.textColor = CNavLightBlackColor;
    _contentLb.font = DYFont(16);
    _contentLb.backgroundColor = UIColor.clearColor;
    [warp addSubview:_contentLb];
    
    
    [_contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemTitleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(warp).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-10);
        make.height.mas_lessThanOrEqualTo(warp.mas_height).mas_offset(-45);
//        make.bottom.equalTo(warp).mas_offset(-15);
    }];
    
    [_contentLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
 
}

- (void)speedVioceClick{
    [[HESpeechManager sharedManager] startWithText: _model.guidance];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.mas_greaterThanOrEqualTo(kScreenHeight-ZXNavBarHeight);
    }];
}

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}


@end
